//
//  BLLiveRoomSingleButton.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/5/30.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit


class BLLiveRoomSingleButton: UIButton {
    
    //顶部占位图层
    private lazy var topPlaceView = UIImageView()
    //手机直播标签图层
    private lazy var topImageView = UIImageView(image: UIImage(named: "home_live_vertical_icon_44x14_"))
    //主播昵称Label
    private lazy var nickNameLabel = UILabel()
    //在线人数Button
    private lazy var onlineBtn = UIButton(type: UIButtonType.Custom)
    //房间名Label
    private lazy var roomNameBtn = UIButton(type: UIButtonType.Custom)
    
    
    var roomID : String = ""
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    /// 取消高亮
    override var highlighted: Bool{
        willSet{
            
        }
    }
    
}

//数据设置
extension BLLiveRoomSingleButton{
    
    /**
     设置房间数据
     
     
     - parameter isPhneLive:   是否是手机直播
     - parameter onlineNumber: 直播人数
     - parameter userName:     主播名字
     - parameter roomName:     房间名字
     
     */
    func setupRoomData(isPhneLive isPhneLive: Bool = false,
                                  onlineNumber : NSNumber?,
                                  userName : String?,
                                  roomName : String?,
                                  roomScrUrl : NSURL?,
                                  roomID : String? = "") {
        //是否为手机直播
        topImageView.hidden = !isPhneLive
        //直播人数
        if onlineNumber != nil {
            SetOnlineNumber(Int(onlineNumber!))
        }
        //主播名字
        if userName != nil {
            nickNameLabel.text = userName
        }
        //房间名
        if roomName != nil {
            roomNameBtn.setTitle(roomName, forState: .Normal)
        }
        if roomScrUrl != nil {
            topPlaceView.layer.cornerRadius = 5
            topPlaceView.clipsToBounds = true
            topPlaceView.sd_setImageWithURL(roomScrUrl)
        }
        if roomID != nil {
            self.roomID = roomID!
        }
    }
    
    /**
     设置在线人数
     
     - parameter onlineNumber: 传入在线人数
     */
    private func SetOnlineNumber(onlineNumber : Int) {
        //设置在线人数Label
        var onLineStr: String  = "\(onlineNumber)在线"
        if Int(onlineNumber) > 10000 {
            let endNumber = CGFloat(onlineNumber) * 0.0001
            onLineStr = String(format: "%0.1f万在线",endNumber)
        }
        onlineBtn.setTitle(onLineStr, forState: .Normal)
    }
    
}


extension BLLiveRoomSingleButton{
    func setupUI() {
        
        //阴影图层
        let shadeImage = UIImage(named: "Image_shade_50x44_")
        let shadeView = UIImageView(image: shadeImage)
        shadeView.alpha = 0.8
        topPlaceView.addSubview(shadeView)
        
        shadeView.snp_makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        
        //左上
        topPlaceView.addSubview(topImageView)
        topImageView.snp_makeConstraints { (make) in
            make.top.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        //左下主播昵称
        nickNameLabel.textColor = UIColor.whiteColor()
        nickNameLabel.font = UIFont.systemFontOfSize(10)
        topPlaceView.addSubview(nickNameLabel)
        nickNameLabel.snp_makeConstraints { (make) in
            make.left.equalToSuperview().offset(4)
            make.bottom.equalToSuperview().offset(-4)
        }
        
        //右下在线人数Button - 默认参数配置
        onlineBtn.setImage(UIImage(named: "Image_online_16x16_" ), forState: .Normal)
        onlineBtn.titleLabel?.font = UIFont.systemFontOfSize(10)
        onlineBtn.adjustsImageWhenDisabled = true
        onlineBtn.sizeToFit()
        topPlaceView.addSubview(onlineBtn)
        
        onlineBtn.snp_makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-4)
            make.right.equalToSuperview().offset(-4)
        }
        
        //底部房间名Button - 默认参数配置
        roomNameBtn.setImage(UIImage(named: "home_live_cate_normal_14x14_" ), forState: .Normal)
        roomNameBtn.titleLabel?.font = UIFont.systemFontOfSize(10)
        roomNameBtn.adjustsImageWhenDisabled = true
        roomNameBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        roomNameBtn.sizeToFit()
        self.addSubview(roomNameBtn)
        
        roomNameBtn.snp_makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.height.equalTo(20)
            make.right.lessThanOrEqualToSuperview()
        }
        
        
        self.addSubview(topPlaceView)
        topPlaceView.snp_makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalTo(roomNameBtn.snp_top)
        }
        
    }
    
    
    
}